﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Diagnostics;

namespace Pg.BioMedics.SDR.Diagnostics
{
    /// <summary>
    /// Used to measure time of the event
    /// </summary>
    [DebuggerDisplay("Category : [{Category}] Name : [{Name}] Type [{Type}]")]
    public class TimingPerformanceCounter : BasePerformanceCounter
    {
        #region Fields

        private Stopwatch counter = new Stopwatch();

        #endregion

        #region Constructors

        /// <summary>
        /// Creates an instance of the TimingCounter
        /// </summary>
        /// <param name="category">Performance counters category</param>
        /// <param name="name">Performance counter name</param>
        internal TimingPerformanceCounter(string category, string name)
            : base(category, name, PerformanceCounterType.NumberOfItems64) 
        { }

        #endregion

        #region Methods

        /// <summary>
        /// Starts timing counting
        /// </summary>
        public void Start()
        {
            lock (this.counter)
            {
                if (!this.counter.IsRunning)
                {
                    this.counter.Reset();
                    this.counter.Start();
                }
            }
        }

        /// <summary>
        /// Stops timing counting
        /// </summary>
        public void Stop()
        {
            lock (this.counter)
            {
                if (this.counter.IsRunning)
                {
                    this.counter.Stop();
                    Apply(this.counter.ElapsedMilliseconds);
                }
            }
        }

        #endregion
    }
}
